//
//  AppDelegate.m
//  ideashareDemo
//
//  Created by huawei on 2021/1/20.
//  Copyright © 2021 huawei. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <netdb.h>
#import <MediaPlayer/MediaPlayer.h>
#import <notify.h>

#define LOCAL_HOST_IP @"127.0.0.1"

extern BOOL isSharing;

@interface AppDelegate ()
{
    SCNetworkReachabilityRef reachabilityRef;
}
@property(assign, nonatomic)NSUInteger bgTask;

@property(strong, nonatomic)AVAudioPlayer *mPlayer;
@property (nonatomic) dispatch_source_t badgeTimer;

@property(strong, nonatomic)NSTimer *mTimer;
@property(assign, nonatomic)UIBackgroundTaskIdentifier backIden;
@property(strong, nonatomic)UIView *coverview;

@property(assign, nonatomic)BOOL isPostStop;
@property(strong, nonatomic)ViewController *viewController;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    signal(SIGPIPE, SIG_IGN);
    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.viewController = [[ViewController alloc]init];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:_viewController];
    
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"ideaShare inter background.");
    if(isSharing == YES) {
        [self comeToBackgroundPlayMusic];
        [self.mPlayer play];
    }
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self.mPlayer pause];
}

- (void)applicationWillTerminate:(UIApplication *)application{
    NSLog(@"ideaShare is Killed！！！！");
    [ViewController finishProjection];
}

-(AVAudioPlayer *)mPlayer{
    if (!_mPlayer) {
       //循环播放音乐 保证后台一直运行该软件，使得该软件一直处于活跃状态
        AVAudioSession *session = [AVAudioSession sharedInstance];
       //打开应用不影响别的播放器音乐
        [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
        [session setActive:YES error:nil];
        //1.音频文件的url路径，实际开发中，用无声音乐
        NSURL *url=[[NSBundle mainBundle]URLForResource:@"HWMWirelessDisplayEshareBg.caf" withExtension:Nil];
        //2.创建播放器（注意：一个AVAudioPlayer只能播放一个url）
        _mPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil];
        //3.缓冲
        [_mPlayer prepareToPlay];
        _mPlayer.numberOfLoops = NSUIntegerMax;
        
        // 注册打断通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AVAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:session];
    }
    return _mPlayer;
}

// 后台播放空白音乐保活
-(void)comeToBackgroundPlayMusic {
    NSLog(@"%s：应用进入后台DidEnterBackground", __FUNCTION__);
    self.bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"123" expirationHandler:^{
       if (self.bgTask != UIBackgroundTaskInvalid) {
           //将要挂起的时候开启播放，让APP在后台保活
           [[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
           self.bgTask = UIBackgroundTaskInvalid;
           [self.mPlayer prepareToPlay];
           BOOL isucc = [self.mPlayer play];
           NSLog(@"isuccisuccisuccisucc:%d",isucc);
       }
    }];
}

- (void)AVAudioSessionInterruptionNotification: (NSNotification *)notificaiton {
    NSLog(@"APP后台播放的空白音乐被打断-重新播放后白空白音乐：%@", notificaiton.userInfo);
    [self comeToBackgroundPlayMusic];
    [self.mPlayer play];
}

@end
